<template>
  <div class="detail-container">
    <!--轮播图banner-->
    <div class="detail-banner">
      <van-swipe :autoplay="5000" @change="onChange">
        <van-swipe-item v-for="item in banner" :key="item.id">
          <van-image fit="cover" :src="item.img_url"></van-image>
        </van-swipe-item>
        <!--自定义指示器-->
        <template #indicator>
          <div class="custom-indicator">{{ current + 1 }}/{{bannerNum}}</div>
        </template>
      </van-swipe>
    </div>
    <!--售后服务-->
    <div class="after-sale">
      <van-row>
        <van-col span="8">
          <van-icon name="checked" />30天无忧退货
        </van-col>
        <van-col span="8">
          <van-icon name="checked" />48小时快速退费
        </van-col>
        <van-col span="8">
          <van-icon name="checked" />满88元免邮费
        </van-col>
      </van-row>
    </div>
    <!--产品信息-->
    <div class="pro-info">
      <div class="title">{{info.name}}</div>
      <div class="brief">{{info.goods_brief}}</div>
      <div class="price-box">
        <div class="price">{{info.retail_price | RMBformat}}</div>
        <div class="sku">库存:{{info.goods_number}}</div>
      </div>
    </div>
    <!--弹出展示-->
    <van-cell title="售后保障" is-link @click="isSkuShow = true" />
    <!--产品规格-->
    <div class="pro-attribute">
      <div class="pro-h2">产品规格</div>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <span class="th">{{item.name}}</span>
          <span class="td">{{item.value}}</span>
        </li>
      </ul>
    </div>
    <!--产品详情-->
    <div class="pro-content">
      <div class="pro-h2">产品详情</div>
      <div class="content" v-html="info.goods_desc"></div>
    </div>
    <!--常见问题-->
    <div class="pro-answer">
      <div class="pro-h2">常见问题</div>
      <div class="list-item">
        <ul>
          <li v-for="(item, index) in issue" :key="index">
            <h3>{{item.question}}</h3>
            <p>{{item.answer}}</p>
          </li>
        </ul>
      </div>
    </div>
    <!--相关产品-->
    <div class="pro-answer">
      <div class="pro-h2">猜你喜欢</div>
      <div class="list-item">
        <Products :goodsList="goodsList" />
      </div>
    </div>
    <!--sku信息-->
    <van-sku ref="sku" v-model="isSkuShow" :sku="sku" :goods="goods" :hide-stock="sku.hide_stock" />
    <!--工具栏-->
    <GoodActive :goodsCount="goodsCount" @addToCart="addToCart" />
  </div>
</template>

<script>
import { GetGoodsDetailData, GetGoodsRelatedData, GetGoodsCountData, PostAddCartData } from '@/request/api'
import Products from '@/components/Products/Products.vue'
import GoodActive from '@/components/GoodActive/GoodActive.vue'
export default {
  name: 'Special',
  components: {
    Products,
    GoodActive
  },
  data () {
    return {
      banner: [], // 轮播图数据
      bannerNum: 1, // 轮播图数量，默认为1
      current: '', // 自定义指示器
      info: [], // 产品信息
      attribute: [], // 产品规格
      issue: [], // 常见问题
      goodsList: [], // 相关产品
      isSkuShow: false, // 默认显示
      sku: {
        tree: [],
        hide_stock: false, // false显示，true为隐藏
        price: '0', // 默认价格显示0
        stock_num: 0 // 默认库存显示0
      },
      goods: {
        picture: '' // 弹窗主图
      },
      goodsCount: 0, // 购物车产品数量
      productList: [] // productList数据
    }
  },
  created () {
    // 产品详情
    GetGoodsDetailData({
      id: this.$route.query.id
    }).then(res => {
      // 发送请求，请求这个商品的详情数据
      // console.log(res)
      // 轮播图图片数据
      this.banner = res.data.gallery
      // 轮播图数量
      this.bannerNum = res.data.gallery.length
      // 产品信息
      this.info = res.data.info
      // 产品规格
      this.attribute = res.data.attribute
      // 常见问题
      this.issue = res.data.issue
      // sku主图数据
      this.goods.picture = res.data.info.list_pic_url
      // sku价格
      this.sku.price = res.data.info.retail_price
      // sku库存数量
      this.sku.stock_num = res.data.info.goods_number
    })
    // 相关产品
    GetGoodsRelatedData({
      id: this.$route.query.id
    }).then(res => {
      // 发送请求，请求这个商品的详情数据
      // console.log(res)
      // 轮播图图片数据
      this.goodsList = res.data.goodsList
    })
    // 购物车产品数量
    GetGoodsCountData().then(res => {
      // console.log(res)
      // 赋值购物车数量
      this.goodsCount = res.data.cartTotal.goodsCount
      // productList数据
      this.productList = res.data.productList
    })
  },
  methods: {
    // 轮播图自定义指示器
    onChange (index) {
      this.current = index
    },
    // 加入购物车
    addToCart () {
      // 1.如果sku没有展示的情况下，点击购物车按钮，展示sku
      // 2.如果sku已经在展示的状态下，点击购物车按钮，跳转到购物车页面
      if (this.isSkuShow) {
        // 加入购物车
        // console.log('执行加入购物车操作')
        // 获取要添加的商品数量 $refs.sku.getSkuData .selectedNum 这个方法是Vant文档里面提供的
        // console.log(this.$refs.sku.getSkuData().selectedNum)
        PostAddCartData({
          goodsId: this.$route.query.id,
          productId: this.productList[0],
          number: this.$refs.sku.getSkuData().selectedNum
        }).then(res => {
          // console.log(res)
          // 添加到购物车后做两件事
          // 1.更新购物车数量
          this.goodsCount = res.data.cartTotal.goodsCount
          // 弹窗提示
          this.$toast.success('加入购物车成功')
          // 2.跳转到购物车页面,1s后跳转
          setTimeout(() => {
            this.$router.push('/cart')
          }, 1000)
        })
      } else {
        // 打开sku面板
        this.isSkuShow = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.detail-banner{
  height: 375px;
  .van-image{
    width: 100%;
    height: 375px;
    display: block;
  }
  .custom-indicator{
    position: absolute;
    right: 15px;
    bottom: 15px;
    background-color: rgba(255, 255, 255, .8);
    font-size: 14px;
    color: #333;
    padding: 0 10px;
    height: 20px;
    line-height: 21px;
    border-radius: 15px;
  }
  /deep/ .van-swipe__indicators{
    bottom: 20px;
    .van-swipe__indicator{
      background-color: #fff;
      opacity: .8;
    }
  }
}
.after-sale{
  width: 100%;
  background-color: #f1f1f1;
  font-size: 14px;
  color: #333;
  padding: 15px 0;
  text-align: center;
  .van-icon{
    margin-right: 4px;
  }
}
.pro-info{
  padding: 20px 10px;
  border-bottom: 10px solid #eee;
  .title{
    font-size: 22px;
    font-weight: bold;
    color: #333;
  }
  .brief{
    font-size: 16px;
    color: #666;
    margin-top: 10px;
  }
  .price-box{
    margin-top: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .price{
      font-size: 22px;
      color: #f00;
    }
    .sku{
      font-size: 14px;
      color: #999;
    }
  }
}
.van-cell{
  padding-left: 10px;
  padding-right: 10px;
}
.pro-h2{
  font-size: 18px;
  color: #333;
  padding: 0 10px;
  line-height: 35px;
  border-bottom: 1px solid #eee;
}
.pro-attribute{
  border-top: 10px solid #eee;
  ul{
    li{
      background-color: #fff;
      display: flex;
      font-size: 14px;
      color: #333;
      line-height: 22px;
      padding: 0 10px;
      span{
        padding: 10px 0;
      }
      .th{
        width: 65px;
        flex-shrink: 0;
      }
      .td{
        width: 100%;
        overflow: hidden;
        word-break: break-all;
      }
    }
    li:nth-child(even){
      background-color: #f1f1f1;
    }
  }
}
.pro-content{
  border-top: 10px solid #eee;
  .pro-h2{
    text-align: center;
    border: none;
    padding-top: 10px;
  }
  .content{
    margin-top: 5px;
    font-size: 16px;
    color: #333;
    word-break: break-all;
    pre{
      padding: 16px;
      overflow: auto;
      background-color: #f6f8fa;
      border-radius: 3px;
    }
    /deep/ img{
      width: 100%;
      display: block;
    }
  }
}
.pro-answer{
  border-top: 10px solid #eee;
  padding-bottom: 20px;
  .pro-h2{
    text-align: center;
    border: none;
    padding-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .pro-h2::before{
    content: '';
    width: 30px;
    height: 1px;
    background-color: #ddd;
    display: inline-block;
    margin-right: 10px;
  }
  .pro-h2::after{
    content: '';
    width: 30px;
    height: 1px;
    background-color: #ddd;
    display: inline-block;
    margin-left: 10px;
  }
  ul{
    padding: 0 10px;
    li{
      padding-left: 10px;
      margin-top: 20px;
      position: relative;
      h3{
        font-size: 16px;
        color: #333;
        line-height: 24px;
        margin-bottom: 5px;
      }
      p{
        font-size: 14px;
        color: #666;
        line-height: 22px;
      }
    }
    li::before{
      content: '';
      width: 6px;
      height: 6px;
      background-color: #333;
      border-radius: 10px;
      position: absolute;
      left: 0;
      top: 8px;
    }
  }
}
.product-container{
  height: auto;
  padding-bottom: 0;
  /deep/ ul{
    padding-bottom: 0;
  }
}
</style>
